<template>
    <div class="personList">
      <el-container>
        <el-header style="text-align: right; font-size: 15px">
          <span>欢迎 {{user.username}}</span>
          <el-dropdown @command="handleCommand">
            <i class="el-icon-setting" style="margin-right: 15px"></i>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>个人信息</el-dropdown-item>
              <el-dropdown-item command="exit">退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-header>
      </el-container>
      <el-card class="box-card" shadow="never">
        <div slot="header" class="clearfix">
          <span>联系人管理</span>
        </div>
        <div class="text item">
          <div class="button-line">
            <el-button type="primary" @click="dialogTableVisible = true">添加好友</el-button>
            <el-button type="primary" @click="delFriend">删除好友</el-button>
            <el-button type="primary" @click="dialogFormVisible = true">添加类别</el-button>
          </div>
          <el-divider></el-divider>
          <el-form :inline="true" :model="formInline" class="demo-form-inline" ref="formInline">
            <el-form-item label="姓名" prop="searchName">
              <el-input v-model="formInline.searchName" placeholder="姓名"></el-input>
            </el-form-item>
            <el-form-item label="类别" prop="searchType">
              <el-select v-model="formInline.searchType" placeholder="类别">
                <el-option v-for="option in options" :label="option.type" :value="option.id"></el-option>
                <el-option value=""></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
          </el-form>
          <el-table :data="tableData" height="500" @selection-change="handleSelectionChangePeople">
            <el-table-column
              type="selection"
              width="55">
            </el-table-column>
            <el-table-column prop="pname" label="姓名" width="120">
            </el-table-column>
            <el-table-column prop="type" label="类型" width="100">
            </el-table-column>
            <el-table-column prop="psex" label="性别" width="100">
            </el-table-column>
            <el-table-column prop="age" label="年龄" width="100">
            </el-table-column>
            <el-table-column prop="mobile" label="手机号" width="150">
            </el-table-column>
            <el-table-column prop="email" label="邮箱" width="150">
            </el-table-column>
            <el-table-column prop="address" label="地址">
            </el-table-column>
            <el-table-column
              fixed="right"
              label="操作"
              width="200">
              <template slot-scope="scope">
                <el-button
                  @click.native.prevent="deleteRow(scope.$index)"
                  type="text"
                  size="small">
                  移除
                </el-button>
                <el-button
                  @click.native.prevent="modifyInfo(scope.$index)"
                  type="text"
                  size="small">
                  修改信息
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <el-pagination
        background
        layout="prev, pager, next"
        :page-size=pageSize
        :total=totalInfo
        @current-change="changePage"
        >
        </el-pagination>
      </el-card>
      <div>
        <el-dialog title="添加好友" :visible.sync="dialogTableVisible" v-if='dialogTableVisible'>
          <add-friend :options="options" :userid="user.id"></add-friend>
        </el-dialog>
      </div>
      <div>
        <el-dialog title="修改好友资料" :visible.sync="modifyVisible">
          <add-friend :options="options" :selectFriend="selectFriend" :modifyVisible="modifyVisible"></add-friend>
        </el-dialog>
      </div>

      <div>
        <el-dialog title="添加类别" :visible.sync="dialogFormVisible">
          <el-form :inline="true" :model="typeForm" class="demo-form-inline" ref="typeForm">
            <el-form-item label="类别名称" prop="typeName">
              <el-input v-model="typeForm.typeName" placeholder="请输入类别名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="addType('typeForm')">添加</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>
      </div>
    </div>
</template>

<style>
  .el-header {
    color: #333;
    line-height: 60px;
  }

  .el-aside {
    color: #333;
  }


  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card{
    width:90%;
    margin:auto;
  }

  .button-line{
    margin:0px 0px 20px 0px;
  }

</style>

<script>
  import addFriend from '~/components/addFriend.vue';
  import axios from "../../plugins/axios";
  import qs from "qs";



  export default {
    components:{
      addFriend:addFriend
    },
    data() {
      return {
        dialogTableVisible:false,
        dialogFormVisible:false,
        modifyVisible:false,
        pageSize:10,
        totalInfo:0,
        typeForm:{
          typeName:''
        },
        tableData: [],
        user:"",
        formInline: {
          searchName: '',
          searchType: ''
        },
        options:[],
        multipleSelection:[],
        selectFriend:""
      }
    },
    methods:{
      searchPerson:function(pageNum){
        let that = this;
        that.$refs['formInline'].validate((valid) => {
          if (valid) {
            let param={
              "typeId":that.formInline.searchType,
              "pname":that.formInline.searchName,
              "pageSize":that.pageSize,
              "pageNum":pageNum
            };
            console.log(param)
            axios.post("/personinfo/searchUserPerson",param)
              .then((res)=>{
                that.tableData = res.data.data.list;
                that.totalInfo = res.data.data.totalNum;
              }).catch((error)=>{
              console.log(error)
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      addFriend:function(){
        this.dialogTableVisible=true
      },
      delFriend:function(){
        let that = this;
        let array = that.multipleSelection;
        console.log(array);
        let formatString="";
        for(let i=0;i<array.length;i++){
          formatString += "&id="+array[i];
        }
        formatString = formatString.substring(1);
        console.log(formatString)
        axios.delete("/personinfo/delUsers?"+formatString)
          .then((res)=>{
            console.log(res)
          }).catch((error)=>{
            console.log(error)
        })
      },
      addType:function(formName){
        let that = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let param={
              "type":that.typeForm.typeName,
              "userId":that.user.id
            };
            console.log(param);
            axios.post("/type/addType",param)
              .then(function (res) {
                that.options.push(res.data.data);
                that.dialogFormVisible = false;
              })
              .catch((error)=>{
                console.log(error);
              })
          } else {
            console.log('error submit!!');
            return false;
          }
        });

      },
      onSubmit:function(){
        this.searchPerson(1);

      },
      changePage:function (page) {
        console.log(page)
        let that = this;
        that.searchPerson(page)
      },
      deleteRow:function(index){
        let that = this;
        that.$confirm('是否删除该用户?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let delUser = that.tableData[index];
          axios.delete("/personinfo/delUser?id="+delUser.id)
            .then((res)=>{
              console.log(res)
              that.$message({
                type: 'success',
                message: '删除成功!'
              });
              setTimeout(function () {
                location.reload()
              },1500)
            }).catch((error)=>{
            console.log(error)
          })
        }).catch((error) => {
          console.log(error)
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleSelectionChangePeople(val){
        console.log("人员信息val-",val);
        this.multipleSelection = [];
        for (let i = 0; i < val.length; i++) {
          if (this.multipleSelection.indexOf(val[i].id) === -1) {
            this.multipleSelection.push(val[i].id)
          }
        }
        console.log("人员信息val--人员选中-",this.multipleSelection);
      },
      modifyInfo:function (index) {
        this.selectFriend = this.tableData[index];
        this.modifyVisible = true;

      },
      handleCommand(command) {
        let that = this;
        if(command==="exit"){
          that.$confirm('是否退出登陆?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            axios.post("/logout")
              .then((res)=>{
                console.log(res);
                that.$message({
                  type: 'success',
                  message: '用户已注销!'
                });
                setTimeout(()=>{
                  that.$router.push("/login/longin")
                },1000000)
              }).catch((error)=>{
              console.log(error)
            })

          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          });
        }

      },
      // exit:function () {
      //   let that = this;
      //   this.$confirm('是否退出登陆?', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(() => {
      //     axios.post("/logout")
      //       .then((res)=>{
      //         that.$message({
      //           type: 'success',
      //           message: '用户已注销!'
      //         });
      //         setTimeout(()=>{
      //           that.$router.push("/login/longin")
      //         },1000)
      //       }).catch((error)=>{
      //         console.log(error)
      //     })
      //
      //   }).catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消删除'
      //     });
      //   });
      // }
    },
    mounted:function () {
      let that = this;
      axios.get("/user/getUserInfo")
        .then((res)=> {
          that.user = res.data.data;
        })
        .catch((error)=>{
          console.log(error)
        })
      that.searchPerson(1);
      axios.get("/type/getUserType")
        .then((res)=>{
          that.options = res.data.data;
        }).catch((error)=>{
        console.log(error)
      })
    },

  };
</script>
